package com.bangf.service.impl;

import com.bangf.domain.*;
import com.bangf.helper.EntityHelper;
import com.bangf.helper.TimeStampHelper;
import com.bangf.repo.HouseholdRepo;
import com.bangf.service.SaveService;
import com.bangf.service.SelectService;
import com.bangf.service.UpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

/**
 * @deprecation 更新服务
 * @email leichuqiao@126.com
 * @author 雷楚桥
 * @date 2018-09-07 18:47
 */
@Service
public class UpdateServiceImpl implements UpdateService {

    @Autowired
    private SelectService selectS;
    @Autowired
    private SaveService saveS;
    @Autowired
    private HouseholdRepo householdR;
    @Autowired
    private EntityHelper entityH;
    @Autowired
    private TimeStampHelper timeStampH;

    @Override
    public void updateHousehold() {

        //查询所有的家庭
        Long count = selectS.countAllHouseHold();
        System.out.println("家庭总数：" + count);
        for(int i=0;i<count;i+=10){
            int sd = (int)((float)i/count * 10000);
            System.out.print("当前进度" + ((float)sd)/100 + "% ");
            List<FpHousehold> householdList = selectS.findHouseHoldLimit(i,10);
            householdList.stream().forEach(household -> {
                //统计家庭总收入
                int manNumber = selectS.countResidentByHouseholdId(household.getId());
                householdR.updateAllIncome(household.getId(),manNumber);

//                统计家庭是否有五保人员，如果有，则将该家庭设置为五保户
                if(selectS.checkResidentWbByHouseholdId(household.getId())) householdR.updateWb(household.getId());
            });

            System.out.println("    统计了" + householdList.size() + "条数据");
        }

    }

    @Override
    public void updateTownSum() {

        //查询所有的镇统计
        List<FpTownSum> townSumList = selectS.findAllTownSum();
        townSumList.stream().forEach(townSum -> {
            int townId = townSum.getTownId();
            FpTown town = selectS.findOneTown(townId);
            townSum.setTownName(town.getTownName());                            //村名更新
            townSum.setPoorVillage(selectS.countPoorVillageByTownId(townId));   //统计这个乡镇的贫困村数量
            townSum = saveS.save(townSum);
            System.out.println("乡镇为：" + townSum.getTownName() + "的已更新");

//            Integer allSum = selectS.sumHouseholdAllIncomeByTownId(townId);
//            Integer householdCount = selectS.countHouseholdByTownId(townId);
//            Integer residentCount = selectS.countResidentByTownId(townId);

//            townSum.setHouseholdCount(householdCount);                                      //已录入家庭数量
//            townSum.setPopulationCount(residentCount);                                      //已录入总人口数
//            townSum.setAllIncome(allSum);                                                   //年总收入
//            townSum.setAvgHouseIncome(allSum/householdCount);                               //家庭年均收入
//            townSum.setAvgUserIncome(allSum/residentCount);                                 //人年均收入
//            townSum.setHelperCount(selectS.countHelperByTownId(townId));                    //帮扶人数
//
//
//            townSum.setCardHousehold(selectS.countCardHouseholdByTownId(townId));           //建档立卡户数量
//            townSum.setAvgNormalHouseIncome(
//                    selectS.countHouseholdAvgIncomeByTownId(1,townId));              //一般户年均收入
//            townSum.setAvgPoorHouseIncome(
//                    selectS.countHouseholdAvgIncomeByTownId(2,townId));              //贫困户年均收入
        });


    }

    @Override
    public void updateTownSatisfy() {
        List<FpTown> townList = selectS.findAllTown();
        System.out.println("正在开始更新乡镇满意度...");
        townList.stream().forEach(town -> {
            Random random = new Random();
            Double fellGood = ((double) (2000 + random.nextInt(2000)))/100;
            Double fellGeneral = ((double) (4000 + random.nextInt(2000)))/100;
            Double fellBad = ((double)((int)((100 - fellGood - fellGeneral) * 100)))/100;
            FpSatisfy satisfy = entityH.createSatisfy(town.getTownId(),fellGood,fellGeneral,fellBad);
            saveS.save(satisfy);
            System.out.println(town.getTownName() + "已更新");
        });
        System.out.println("更新乡镇满意度已完成");
    }

    @Override
    public void updateTownEvaluation() {
        List<FpTown> townList = selectS.findAllTown();
        System.out.println("正在开始更新乡镇满评价表...");
        townList.stream().forEach(town -> {
            Random random = new Random();
            Double fellGood = ((double) (2000 + random.nextInt(2000)))/100;
            Double fellGeneral = ((double) (4000 + random.nextInt(2000)))/100;
            Double fellBad = ((double)((int)((100 - fellGood - fellGeneral) * 100)))/100;
            FpEvaluation evaluation = entityH.createEvaluation(town.getTownId(),fellGood,fellGeneral,fellBad);
            saveS.save(evaluation);
            System.out.println(town.getTownName() + "已更新");
        });
        System.out.println("更新乡镇评价表已完成");
    }


    @Override
    public void updateAllSum() {
        FpAllSum sum = selectS.findOneSum();
        List<FpTownSum> townSumList = selectS.findAllTownSum();
        System.out.println("正在开始更新扶贫总统计...");
        for(FpTownSum townSum : townSumList){

            sum.setHouseholdCount(sum.getHouseholdCount() + townSum.getHouseholdCount());
            sum.setPopulationCount(sum.getPopulationCount() + townSum.getPopulationCount());
            sum.setArea(sum.getArea() + townSum.getArea());
            sum.setNyhjhs(sum.getNyhjhs() + townSum.getNyhjhs());
            sum.setNyhjrs(sum.getNyhjrs() + townSum.getNyhjrs());
            sum.setMan(sum.getMan() + townSum.getMan());
            sum.setWomen(sum.getWomen() + townSum.getWomen());
            sum.setNation(sum.getNation() + townSum.getNation());
            sum.setPoorVillage(sum.getPoorVillage() + townSum.getPoorVillage());
            sum.setPoorHousehold(sum.getPoorHousehold() + townSum.getPoorHousehold());
            sum.setPoorPopulation(sum.getPoorPopulation() + townSum.getPoorPopulation());
            sum.setDbHousehold(sum.getDbHousehold() + townSum.getDbHousehold());
            sum.setDbPopulation(sum.getDbPopulation() + townSum.getDbPopulation());
            sum.setLbHousehold(sum.getLbHousehold() + townSum.getLbHousehold());
            sum.setLbPopulation(sum.getLbPopulation() + townSum.getLbPopulation());
            sum.setLdlrs(sum.getLdlrs() + townSum.getLdlrs());
            sum.setWnrs(sum.getWnrs() + townSum.getWnrs());
            sum.setZyjyrs(sum.getZyjyrs() + townSum.getZyjyrs());
            sum.setCyrs(sum.getCyrs() + townSum.getCyrs());
            sum.setNjlCount(sum.getNjlCount() + townSum.getNjlCount());
            sum.setHzsCount(sum.getHzsCount() + townSum.getHzsCount());
            sum.setCjrks(sum.getCjrks() + townSum.getCjrks());
            sum.setWbrks(sum.getWbrks() + townSum.getWbrks());

            System.out.println("统计中...");
        }
        sum.setUpdateTime(timeStampH.getTimeStamp());

        saveS.save(sum);
        System.out.println("更新扶贫总统计已完成");
    }

    @Override
    public void updateTownMonthThan() {
        List<FpTownSum> townSumList = selectS.findAllTownSum();
        System.out.println("正在开始更新更新乡镇与上月比较数据...");
        townSumList.stream().forEach(townSum -> {
            FpThanMonth thanMonth = entityH.createThanMonth(townSum);
            saveS.save(thanMonth);
            System.out.println("统计中...");
        });
        System.out.println("更新乡镇与上月比较数据已完成");
    }


    @Override
    public void updateResidentParty() {
        Long count = selectS.countResident();
        System.out.println("人员总数：" + count);
        Random random = new Random();
        for(int i=0;i<count;i+=10){
            int sd = (int)((float)i/count * 10000);
            System.out.println("当前进度" + ((float)sd)/100 + "% ");
            List<FpResident> residentList = selectS.findResidentLimit(i,10);

            residentList.stream().forEach(resident -> {
                int rd = random.nextInt(100);
                if(rd < 8){
                    resident.setIsParty(1);
                    saveS.save(resident);
                }
            });
        }
        System.out.println("统计完成");
    }


    @Override
    public void updateVillageSum() {
        //查询所有的镇统计
        List<FpVillageSum> FpVillageSum = selectS.findAllVillageSum();
        FpVillageSum.stream().forEach(villageSum -> {
            int villageId = villageSum.getVillageId();
            Integer allIncome = selectS.sumHouseholdAllIncomeByVillageId(villageId);
            if(allIncome == null) allIncome = 0;
//            villageSum.setAllIncome(allIncome);
            FpVillage village = selectS.findOneVillage(villageId);
            if(village != null)
                villageSum.setVillageName(village.getVillageName());
            villageSum = saveS.save(villageSum);
            System.out.println("村庄为：" + villageSum.getVillageName() + "的已更新");
        });
    }


}
